import random
from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base

url = 'http://192.168.8.222/admin.php'


class Phone2(Base):
    # 获取cookies
    def login(self):
        self.save_cookie('../data/w_warehouse_cookies.json')
        sleep(20)

    def login_admin(self):
        self.set_cookie('../data/w_warehouse_cookies.json')
        self.driver.get(url)
        self.click((By.XPATH, '//*[@id="admin-offcanvas"]/div/ul/li[11]/a'))
        # 点击导航管理，进入iframe
        self.click((By.XPATH, '//*[@id="power-menu-319"]/li[2]/a'))
        self.go_iframe((By.ID, "ifcontent"))

    def add_procedure(self, name, eventvalue, order):
        # 新增
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[1]'))
        # 输入名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 点击安卓平台
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div/div/ul/li[4]'))
        # 选择事件类型
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div/div/ul/li[2]'))
        # 输入事件值
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), eventvalue)
        sleep(15)
        # 设置背景色
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/button'))

        datas = self.driver.find_elements(By.XPATH, '//*[@id="CT"]/tbody/tr')
        arr = []
        for data in datas:
            arr.extend(data.find_elements(By.XPATH, '//td'))
        # print(arr)
        # print(len(arr))
        arr[random.randint(0, len(arr) - 1)].click()
        sleep(2)
        # 设置顺序
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[7]/input'), order)
        # 是否默认，是否启用
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[8]/div/div/span[3]'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[9]/div/div/span[3]'))
        # 点击保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[10]/button'))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))

    def delete_procedure(self):
        # 勾选
        self.click((By.CSS_SELECTOR, 'td:nth-child(1) > div > label'))
        # 删除
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[1]'))
        # 确认
        self.click((By.CSS_SELECTOR, 'div > div.am-modal-footer > span:nth-child(2)'))

    def reset_search_procedure(self):
        # 点击输入框，选择小程序
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/table/thead/tr[2]/td[3]/div/div/ul/li[6]'))
        # 点击搜索
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/button[2]'))
        sleep(1)
        # 重置
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/a[2]'))
        sleep(1)

    def sidebar_procedure(self, name, value, order):
        # 详情
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-default.am-btn-xs.am-radius.am-btn-block.submit-popup'))
        sleep(1)
        # 退出详情
        self.click((By.XPATH, '//div[starts-with(@id,"am-modal-")]/div/span'))
        # 编辑
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > a'))
        # 名称
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[1]/input'), name)
        # 所属平台
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div/a'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[2]/div/div/ul/li[4]'))
        # 事件类型
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div'))
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[3]/div/div/ul/li[2]'))
        # 事件值
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[4]/input'), value)
        sleep(30)
        # 设置背景色
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[6]/button'))
        datas = self.driver.find_elements(By.XPATH, '//*[@id="CT"]/tbody/tr')
        arr = []
        for data in datas:
            arr.extend(data.find_elements(By.XPATH, '//td'))
        arr[random.randint(0, len(arr) - 1)].click()
        sleep(2)
        # 设置顺序
        self.input((By.XPATH, '/html/body/div[2]/div/form/div[7]/input'), order)
        # 保存
        self.click((By.XPATH, '/html/body/div[2]/div/form/div[10]/button'))
        sleep(1)
        # 删除
        self.click((By.CSS_SELECTOR,
                    'td.am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-danger.am-btn-xs.am-radius.am-btn-block.submit-delete'))
        self.click((By.CSS_SELECTOR, 'div > div.am-modal-footer > span:nth-child(2)'))
        print(self.get_text((By.XPATH, '//*[@id="common-prompt"]/p')))
        return self.get_text((By.XPATH, '//*[@id="common-prompt"]/p'))


if __name__ == '__main__':
    p = Phone2()
    p.login_admin()
    # p.add_procedure('12', '123546', '2')
    # p.delete_procedure()
    # p.reset_search_procedure()
    # p.sidebar_procedure('123', '456', '2')
